package hu.raholec.controller;

import hu.raholec.data.Content;
import hu.raholec.data.JsonRequest;
import hu.raholec.service.ContentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class ShowController {

    @Autowired
    private ContentService service;

    @RequestMapping("index")
    public String index(Model model) {
        return "index";
    }

    @RequestMapping(value = "add", method = RequestMethod.GET)
    public String add(Model model,
            @RequestParam("path") String path,
            @RequestParam("nodename") String nodename) {

        service.addNode(path, nodename);
        return get(model, path);
    }

    @RequestMapping(value = "get", method = RequestMethod.GET)
    public String get(Model model,
            @RequestParam("path") String path) {

        Content c = service.getContent(path);
        model.addAttribute("content", c);
        return "content";
    }

    @RequestMapping(value = "set", method = RequestMethod.GET)
    public String setproperty(Model model,
            @RequestParam("path") String path,
            @RequestParam("propname") String propname,
            @RequestParam("propval") String propval) {

        service.addProperty(path, propname, propval);
        return get(model, path);
    }

    @RequestMapping("getContextJson.json")
    public @ResponseBody Content getContext(@RequestParam String path) {
        System.out.println("getContextJson running...");
        Content c = service.getContent(path);
        System.out.println("Content: " + c);
        return c;
    }
}
